﻿using QuanLyThuPhiTaiLieu.Biz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class SelectBanVe : Form
    {
        public string Result { get; set; }

        public SelectBanVe(int danhMucId, int khaiThacId, int thamKhaoId, string data)
        {
            InitializeComponent();

            LoadData(danhMucId, khaiThacId, thamKhaoId, data);
        }

        private void LoadData(int danhMucId, int khaiThacId, int thamKhaoId, string data)
        {
            var currentData = new List<string>();
            if (!string.IsNullOrEmpty(data))
            {
                var selectedItems = data.Split(';');
                foreach (var selectedItem in selectedItems)
                {
                    currentData.Add(selectedItem);
                }
            }

            cblNoiDung.Items.Clear();

            var biz = new DanhMucBoSungBiz();
            var items = biz.GetAllBanVe(danhMucId, thamKhaoId, khaiThacId);
            if (items != null && items.Count > 0)
            {
                foreach (var bosung in items)
                {
                    cblNoiDung.Items.Add(bosung.NoiDung);
                    if (currentData.Contains(bosung.NoiDung))
                    {
                        cblNoiDung.SetItemChecked(cblNoiDung.Items.Count - 1, true);
                    }
                }
            }
        }

        private void Select(object sender, EventArgs e)
        {
            string temp = string.Empty;

            for (int i = 0; i < cblNoiDung.Items.Count; i++)
            {
                if (cblNoiDung.GetItemChecked(i))
                {
                    string str = (string)cblNoiDung.Items[i];
                    temp = string.Concat(temp, str, ";");
                }
            }

            if (temp.EndsWith(";"))
            {
                temp = temp.Substring(0, temp.Length - 1);
            }

            Result = temp;

            this.Close();
        }

        private void Cancel(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
